package org.one.stone.soup.grfxML.plugin.widget;

import org.one.stone.soup.grfxML.DataNode;
import org.one.stone.soup.grfxML.DataPoint;
import org.one.stone.soup.grfxML.DataScale;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.NameHelper;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.LockException;

public class ScrollBar extends org.one.stone.soup.grfxML.plugin.SimplePlugin {
	public static final String HORIZ="H";
	public static final String VERT="V";

	private static final int HORIZ_MODE	=1;
	private static final int VERT_MODE	=2;
	private int MODE = 0;

	public static final int ARG_TYPE	= 0;
	public static final int ARG_SOURCE	= 1;
	public static final int ARG_TARGET	= 2;
	public static final int ARG_SCALE	= 3;

	DataString type = new DataString();
	DataPoint source = new DataPoint();
	DataString targetName = new DataString();
	DataScale scale = new DataScale();

	DataNode target;
	DataPoint targetPoint;
	int offsetX;
	int offsetY;
/**
 * ScrollBar constructor comment.
 */
public ScrollBar(GrfxMLEngine engine) {
	super(engine);
}
public void initialize()
{
	if(type==null || type.getValue().length()==0)
		System.out.println("Scrollbar type not set");
	else if(type.getValue().equals(HORIZ))
		MODE = HORIZ_MODE;
	else if(type.getValue().equals(VERT))
		MODE = VERT_MODE;

	target = (DataNode)getEngine().getAPI().getField(targetName.getValue());
	if(target==null)
		System.out.println("Scrollbar target with name "+targetName.getValue()+" not found.");
	targetPoint = target.getTranslation().getPosition();

	String scaleName = scale.getId();
	if(scaleName!=null && !NameHelper.isSystemName(scaleName))
	{
		DataScale newScale = (DataScale)getEngine().getAPI().getField(scaleName);
		if(newScale!=null)
			scale = newScale;
	}
	System.out.println("Scale set as "+scale.getValue());

	offsetX = targetPoint.getValueX();
	offsetY = targetPoint.getValueY();
}
public void process()
{
try{
	if(MODE == VERT_MODE)
	{
		int y = (int) ( (source.getValueY()-source.getValueYMin()) *scale.getValue());

		targetPoint.setValueY(
			offsetY-y
			,this
		);
	}
	else if(MODE == HORIZ_MODE)
	{
		int x = (int) ( (source.getValueX()-source.getValueXMin()) *scale.getValue());

		targetPoint.setValueX(
			offsetX-x
			,this
		);
	}
}catch(LockException le){}//dummy catch
}
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	type 		=	grfxMLCaster.cast(type,getArg(type,ARG_TYPE,store));
	source 		=	grfxMLCaster.cast(source,getArg(source,ARG_SOURCE,store));
	targetName	=	grfxMLCaster.cast(targetName,getArg(targetName,ARG_TARGET,store));
	scale		=	grfxMLCaster.cast(scale,getArg(scale,ARG_SCALE,store));
}
public void replace(org.one.stone.soup.mjdb.data.field.Field oldObj, org.one.stone.soup.mjdb.data.field.Field newObj)
{
	type		=	grfxMLCaster.replace(type,oldObj,newObj);
	source		=	grfxMLCaster.replace(source,oldObj,newObj);
	targetName	=	grfxMLCaster.replace(targetName,oldObj,newObj);
	scale		=	grfxMLCaster.replace(scale,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
